﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using KorbeRTR.Utility;

namespace KorbeRTR
{
    public partial class FormDeviceParameters : Form
    {
        public Models.OmronData plc => FormMainWindow.OmronPLC;

        public FormDeviceParameters()
        {
            InitializeComponent();
        }

        private void FormDeviceParameters_Load(object sender, EventArgs e)
        {
            //定时器里面各种状态刷新       
            timer1.Enabled = true;
            InitGrid(grid1);

            UpdateGrid(grid1);

            UpdateValue();
        }


        /// <summary>
        /// 初始化grid
        /// </summary>
        /// <param name="grid"></param>
        public void InitGrid(FlexCell.Grid grid)
        {
            //Grid1设置
            grid.AutoRedraw = false;
            grid.ExtendLastCol = false;

            grid.DisplayRowNumber = true;

            // 设置行数/列数
            grid.Rows = 3+2*Config.镀铜槽数;
            grid.Cols = 3;

            grid.Column(0).Width = 30;
            grid.Column(1).Width = 130;
            //这个包围区间要减1 居中
            grid.Range(0, 0, grid.Rows - 1, grid.Cols - 1).Alignment = FlexCell.AlignmentEnum.CenterCenter;
            // 设置单元格文字
            int col = 0;
            grid.Cell(0, col++).Text = "序号";
            grid.Cell(0, col++).Text = "标签";
            grid.Cell(0, col++).Text = "设定";

            int row = 1;
            for (int i = 1; i <= Config.镀铜槽数; i++)
            {
                grid.Cell(row++, 1).Text = "镀铜" + i.ToString() + "-1频率(Hz)";
                grid.Cell(row++, 1).Text = "镀铜" + i.ToString() + "-2频率(Hz)";
            }
          
            grid.Column(2).CellType = FlexCell.CellTypeEnum.Button;
        
            grid.AutoRedraw = true;
            grid.Refresh();
        }

        private void UpdateGrid(FlexCell.Grid grid)
        {
            grid.AutoRedraw = false;

            var plc = FormMainWindow.OmronPLC;

            int row = 1;
            for (int i = 1; i <= Config.镀铜槽数; i++)
            {
                grid.Cell(row++, 2).Text = plc.D镀铜槽状态[i - 1].设定频率[0].Value.ToString();
                grid.Cell(row-1, 2).ForeColor = Color.Black;
                grid.Cell(row++, 2).Text = plc.D镀铜槽状态[i - 1].设定频率[1].Value.ToString();
                grid.Cell(row - 1, 2).ForeColor = Color.Black;
            }
       
            grid.AutoRedraw = true;
            grid.Refresh();
        }

        private double Min镀铜频率 = 5;
        private double Max镀铜频率 = 50;
        private double Min除油频率 = 5;
        private double Max除油频率 = 50;


         

        /// <summary>
        /// 设备参数设定
        /// </summary>
        /// <param name="Sender"></param>
        /// <param name="e"></param>
        private void grid1_ButtonClick(object Sender, FlexCell.Grid.ButtonClickEventArgs e)
        {
            int row = Utility.AuxLib.Bound(1, e.Row, grid1.Rows - 1);
            int col = Utility.AuxLib.Bound(1, e.Col, grid1.Cols - 1);

            string unit = "Hz";
            double min = grid1.Cell(row, 1).Text.Contains("铜")? Min镀铜频率 : Min除油频率;
            double max = grid1.Cell(row, 1).Text.Contains("铜") ? Max镀铜频率 : Max除油频率; ;

            string msg = string.Format("请输入'{0}':\n(范围:{1}-{2},单位:{3})", grid1.Cell(row, 1).Text, min, max, unit);

            bool bl = CheckForm(typeof(FormInfo).Name);
            if (!bl)
            {
                FormInfo formInfo = new FormInfo(msg, min, max);
                //   formLogin.MdiParent = this;
                formInfo.ShowDialog();
                if (formInfo.ok)
                {
                    var input = formInfo.input;
                    grid1.Cell(row, col).Text = input;
                    grid1.Cell(row, col).ForeColor = Color.Red;

                    if(1<=row && row<=2*Config.镀铜槽数)
                    {
                        int index = (row - 1) / 2;
                        int subindex = (row - 1) & 1;

                        //MessageBox.Show(index.ToString() + " " + subindex.ToString());
                        plc.D镀铜槽状态[index].设定频率[subindex].Value = input.ToDouble(); 
                    }
                    else
                    {

                    }

                    

                }
            }


        }

        //检测窗体是否打开
        private bool CheckForm(string formName)
        {
            bool bl = false;
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == formName)
                {
                    bl = true;
                    f.Activate();
                    break;
                }
            }
            return bl;
        }


        private void btn电流写入PLC_Click(object sender, EventArgs e)
        {
            
            plc.BH保护电流时间.Value = txt保护电流时间.Text.ToDouble();
            plc.BH保护电流百分比.Value = txt保护电流百分比.Text.ToDouble();
        }


        private void UpdateValue()
        {
            var plc = FormMainWindow.OmronPLC;

            txt保护电流时间.Text = plc.BH保护电流时间.Value.ToString();
            txt保护电流百分比.Text = plc.BH保护电流百分比.Value.ToString();

            txt镀铜速度.Text = plc.SD设定线速.Value.ToString("f2");

        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            UpdateValue();
            UpdateGrid(grid1);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        

        private void btn保护电流开关_Click(object sender, EventArgs e)
        {
//            plc.SetB保护电流开关.Value = !plc.SetB保护电流开关.Value;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (true )//plc.SetB保护电流开关.Value)
            {
                btn保护电流开关.BackColor = Color.Red;

            }
            else
            {
                btn保护电流开关.BackColor = Color.Lime;
            }

            if (plc.WS维修保养开关.Value)
            {
                btn设备维修开关.BackColor = Color.Lime;
            }
            else
            {
                btn设备维修开关.BackColor = Color.Red;
            }
        }


        private void txt保护电流时间_MouseClick(object sender, MouseEventArgs e)
        {
            string name = "保护电流时间";
            string type = "";
            string unit = "秒";
            double min = 0;
            double max = 100;

            string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

            bool bl = CheckForm(typeof(FormInfo).Name);
            if (!bl)
            {
                FormInfo formInfo = new FormInfo(msg, min, max);

                formInfo.ShowDialog();
                if (formInfo.ok)
                {
                    (sender as TextBox).Text = formInfo.input;
                }
            }
        }

        private void txt保护电流百分比_MouseClick(object sender, MouseEventArgs e)
        {
            string name = "保护电流百分比";
            string type = "";
            string unit = "";
            double min = 0;
            double max = 100;

            string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

            bool bl = CheckForm(typeof(FormInfo).Name);
            if (!bl)
            {
                FormInfo formInfo = new FormInfo(msg, min, max);

                formInfo.ShowDialog();
                if (formInfo.ok)
                {
                    (sender as TextBox).Text = formInfo.input;
                }
            }
        }

        private void txt铜循环1使用时间_MouseClick(object sender, MouseEventArgs e)
        {
            string name = "铜循环泵1使用时间";
            string type = "";
            string unit = "秒";
            double min = 0;
            double max = 100;

            string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

            bool bl = CheckForm(typeof(FormInfo).Name);
            if (!bl)
            {
                FormInfo formInfo = new FormInfo(msg, min, max);

                formInfo.ShowDialog();
                if (formInfo.ok)
                {
                    (sender as TextBox).Text = formInfo.input;
                }
            }
        }

        private void txt铜循环1停止时间_MouseClick(object sender, MouseEventArgs e)
        {
            string name = "铜循环泵1停止时间";
            string type = "";
            string unit = "秒";
            double min = 0;
            double max = 100;

            string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

            bool bl = CheckForm(typeof(FormInfo).Name);
            if (!bl)
            {
                FormInfo formInfo = new FormInfo(msg, min, max);

                formInfo.ShowDialog();
                if (formInfo.ok)
                {
                    (sender as TextBox).Text = formInfo.input;
                }
            }
        }

        private void txt镀铜速度_MouseClick(object sender, MouseEventArgs e)
        {

        }

        private void txt板高正补偿_MouseClick(object sender, MouseEventArgs e)
        {
            string name = "板高正补偿";
            string type = "";
            string unit = "mm";
            double min = 0;
            double max = 100;

            string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

            bool bl = CheckForm(typeof(FormInfo).Name);
            if (!bl)
            {
                FormInfo formInfo = new FormInfo(msg, min, max);

                formInfo.ShowDialog();
                if (formInfo.ok)
                {
                    (sender as TextBox).Text = formInfo.input;
                }
            }
        }

        private void txt板高负补偿_MouseClick(object sender, MouseEventArgs e)
        {
            string name = "板高负补偿";
            string type = "";
            string unit = "mm";
            double min = 0;
            double max = 100;

            string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

            bool bl = CheckForm(typeof(FormInfo).Name);
            if (!bl)
            {
                FormInfo formInfo = new FormInfo(msg, min, max);

                formInfo.ShowDialog();
                if (formInfo.ok)
                {
                    (sender as TextBox).Text = formInfo.input;
                }
            }
        }

        private void btn设备维修开关_Click(object sender, EventArgs e)
        {
            plc.WS维修保养开关.Value = !plc.WS维修保养开关.Value;
        }

        private void btn板高写入PLC_Click(object sender, EventArgs e)
        {

        }

        private void btn镀铜速度写入PLC_Click(object sender, EventArgs e)
        {
            plc.SD设定线速.Value = txt镀铜速度.Text.ToDouble() ;
        }
    }
}
